package lianxi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Sort {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		List users = new ArrayList();
		User user1 = new User(12, "小萝莉");
		User user2 = new User(46, "大叔");
		User user3 = new User(78, "大爷");
		users.add(user3);
		users.add(user1);
		users.add(user2);
		Collections.sort(users, new Comparator() {

			@Override
			public int compare(Object o1, Object o2) {
				// TODO Auto-generated method stub
				return ((User) o1).getAge() - ((User) o2).getAge();
			}
			// o1 - o2 是升序,o2-o1是降序
		});
		for (Object object : users) {
			System.out.println(object);
		}
	}
}

class User {
	private String name;
	private int age;

	public User(int age, String name) {
		super();
		this.age = age;
		this.name = name;
	}

	public User() {
		super();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "姓名 : " + name + " , 年龄 : " + age;
	}
}